// 审核计划表
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="年份" prop="planYear">
        <el-date-picker type="year" v-model="queryParams.planYear" format="yyyy" value-format="yyyy"
          :style="{ width: '100%' }" placeholder="请选择年度" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['inAudit:auditPlan:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['inAudit:auditPlan:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="printBox"
          v-hasPermi="['inAudit:auditPlan:print']">打印</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table @selection-change="handleSelectionChange" fit v-loading="loading" border :data="dataList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column show-overflow-tooltip label="流程状态" width="100">
        <template slot-scope="scope">
          <el-tag size="mini">{{ scope.row.statusName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="200" show-overflow-tooltip label="年度">
        <template slot-scope="scope">
          <span>{{ scope.row.planYear }}年度内部工作计划</span>
        </template>
      </el-table-column>
      <el-table-column min-width="200" show-overflow-tooltip label="内审时间">
        <template slot-scope="scope">
          <span>{{ scope.row.startAuditDate }}~{{ scope.row.endAuditDate }}</span>
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="年份" prop="planYear" min-width="120" />
      <el-table-column show-overflow-tooltip label="审核要素" prop="element" min-width="120" />
      <el-table-column show-overflow-tooltip label="被审核部门" prop="deptName" min-width="120" />

      <el-table-column show-overflow-tooltip label="内审组成员" min-width="150" prop="leaderName">
        <template slot-scope="scope">
          {{ scope.row.userList.map((e) => e.userName).toString() }}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="内审组组长" min-width="120" prop="leaderName" />
      <el-table-column label="是否启用" v-if="$auth.hasPermi('inAudit:auditPlan:status:change')" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.effectStatus" :active-value="1" :inactive-value="0"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['inAudit:auditPlan:info']"
            @click="handleDetail(scope.row)">详情</el-button>
          <el-button
            v-if="scope.row.status === 'draft'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['inAudit:auditPlan:edit']"
            >修改</el-button
          >
          <el-button
            v-if="scope.row.status === 'draft'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['company:certificate:remove']"
            >删除</el-button
          >
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)"
            v-hasPermi="['inAudit:auditPlan:add:corrent']">
            <el-button v-if="scope.row.status === 'end'" size="mini" type="text"
              icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <!-- <el-dropdown-item
                command="handleDelete"
                icon="el-icon-delete"
                v-hasPermi="['company:certificate:remove']"
                >删除</el-dropdown-item
              > -->
              <el-dropdown-item v-hasPermi="['inAudit:auditPlan:add:corrent']" command="addLogConent"
                icon="el-icon-plus">新增内审核查</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 打印模板位置 -->
    <PrintBox :printRow="printList" :isPageSize="false" :checkNum="6" :checkLabel="'itemList'" id="printBox">
      <template slot="title" slot-scope="scope">
        <div style="margin-bottom: 40px">
          管理体系内部审查计划表 STET/B/12-1
        </div>
      </template>
      <template slot-scope="scope">
        <!-- 表格  class  table-page  必须-->
        <table style="border-collapse: collapse; table-layout: fixed" border="1" cellspacing="0" class="table-page"
          :isVertical="false">
          <tr>
            <td rowspan="5" style="width: 4mm">内部审查</td>
            <td style="width: 50mm">内审时间</td>
            <td style="width: 100mm">
              {{ parseTime(scope.item.startAuditDate, "{y}年{m}月{d}日") }}至{{
                parseTime(scope.item.endAuditDate, "{y}年{m}月{d}日")
              }}
            </td>
            <td style="width: 50mm">备注</td>
          </tr>
          <tr>
            <td>审查要素</td>
            <td>
              <div style="text-align: left;">
                <div v-for="lab in scope.item.element.split(',')">
                  {{ lab }}
                </div>
              </div>
            </td>
            <td>{{ scope.item.remark }}</td>
          </tr>
          <tr>
            <td>被审核部门</td>
            <td colspan="2">{{ scope.item.deptName }}</td>
          </tr>
          <tr>
            <td>内审组组长</td>
            <td colspan="2">{{ scope.item.leaderName }}</td>
          </tr>
          <tr>
            <td>内审组成员</td>
            <td colspan="2">
              <div style="text-align: left;">
                <div v-for="(lab,val) in scope.item.userList">
                  <div>
                    <span>
                    第{{ val + 1 }}小组：
                  </span>
                  <span style="margin-left: 5px;">
                    {{ lab.userName }}
                  </span>
                  </div>
                  <div>
                    被审核部门：{{ lab.deptName }}
                  </div>
                </div>
              </div>

            </td>
          </tr>
          <tr>
            <td rowspan="6" style="width: 4mm">具体工作安排</td>
            <!-- <td style="width: 50mm">
              {{ scope.item.itemList[0] && scope.item.itemList[0].userName }}
            </td> -->
            <!-- <td colspan="2">
              {{ scope.item.itemList[0] && scope.item.itemList[0].doWork }}
            </td> -->
          </tr>
          <tr v-for="(e, i) in  scope.item.itemList">
            <td style="width: 50mm">
              <!-- {{
                scope.item.itemList[i + 1] &&
                scope.item.itemList[i + 1].userName
              }} -->
              {{ e.startTime }}~{{ e.endTime }}
            </td>
            <td colspan="2">
              {{
                e.doWork
              }}
            </td>
          </tr>
        </table>
        <!-- 底部信息位置 -->
        <div class="desc" style="display: flex;
            align-items: center;
            justify-content: space-around;
            margin-top: 25px;
            }">
          <div style="position: relative">
            编制：
            <img style="width: 80px; position: absolute; top: -10px" :src="$VUE_APP_IMG_API + scope.item.esign" />
          </div>
          <div>日期：{{ scope.item.recordTime }}</div>
          <div style="position: relative">
            批准：<template v-if="CheckApprove(scope, 'zjl').ShowImg">
              <img style="width: 80px; position: absolute; top: -10px" :src="CheckApprove(scope, 'zjl').ApproveImg" />
            </template>
          </div>
          <div>日期：{{ checkHistoryNode(scope, "zjl").commentList[0].time.substring(0, 11) || '' }}</div>
        </div>
        <div class="page-bottom" style="page-break-after: always">
          <div class="desc">注：本表格自2021年6月1日起生效</div>
        </div>
      </template>
    </PrintBox>
  </div>
</template>

<script>
import {
  listauditPlan,
  delauditPlan,
  effectauditPlan,
} from "@/api/quality/inAudit.js";
import printStyle from "@/utils/print.js";

export default {
  name: "auditPlan",
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planYear: null,
      },
      // 日期范围
      dateRange: [],
      infos: [], //勾选的数据
      printList: [],
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    console.log(JSON.parse(str));
    if (str) {
      this.queryParams = JSON.parse(str);
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
  },
  methods: {
    printBox() {
      //infos  勾选的数据 如果有勾选就打印勾选。
      if (this.infos.length) {
        this.printList = this.infos;
      } else {
        //反之打印当前整个列表
        this.printList = this.dataList;
      }
      setTimeout(() => {
        document.title = "管理体系内部审查计划表 STET/B/12-1";
        this.$print({
          printable: "printBox", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyle(),
          onPrintDialogClose: () => {
            document.title = process.env.VUE_APP_TITLE;
          },
        });
      }, 500);
    },
    // 可以自定义样式  printStyle 是公共样式 切勿私自修改
    workStyle() {
      return `${printStyle}
        table,
        table tr th,
        table tr td {
          height:10mm;
          padding: 4px;
        }
        .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
            .df {
                display: flex;
              }
      `;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.innerAuditPlanId);
      this.single = selection.length != 1;
      this.infos = selection;

      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const configId = row.innerAuditPlanId || this.ids;
      this.$router.push("./addAuditPlan?isEdit=true&id=" + configId);
    },
    handleDetail(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      // this.$router.push(
      //   "./addAuditPlan?isInfo=true&id=" + row.innerAuditPlanId
      // );
      this.$router.push({
        path: "/auditPlan/process/detail/" + row.piid,
        query: {
          definitionId: null,
          deployId: null,
          taskId: null,
          finished: false,
          id: row.innerAuditPlanId,
        },
      });
      // this.$router.push("./addBuy?isInfo=true&id=" + row.deviceBuyId);
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleDelete":
          this.handleDelete(row);
          break;
        case "addLogConent":
          this.addLogConent(row);
          break;
        default:
          break;
      }
    },
    addLogConent(row) {
      this.$router.push("./addInAudit?planId=" + row.innerAuditPlanId);
    },
    handleStatusChange(row) {
      let text = row.effectStatus == "1" ? "启用" : "禁用";
      this.$modal
        .confirm(
          '确认要"' + text + '""' + row.planYear + '"年度内部工作计划数据吗？'
        )
        .then(function () {
          return effectauditPlan(row.innerAuditPlanId, row.effectStatus);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.effectStatus = row.effectStatus == "0" ? "1" : "0";
        });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.innerAuditPlanId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "innerAuditPlanId",
        "DELETE",
        "year"
      );
      this.$modal
        .confirm('是否确认删除id为"' + configId + '"的数据项？')
        .then(function () {
          return delauditPlan(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleAdd() {
      this.$router.push("./addAuditPlan");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listauditPlan(
        this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startAuditDate",
          "endAuditDate"
        )
      ).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleExport() {
      this.download(
        "system/auditPlan/export",
        {
          ...this.queryParams,
        },
        `内部审核计划记录表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
